﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Client.Helper;
using Zwms.Data.CommonObject;

namespace Zwms.Client.ManageWms
{
    public partial class StockQueryWnd : UserControl
    {
        #region 成员变量
        private DateTime _planDate;
        private PagerIndex _pagerIndex;
        #endregion

        #region 构造函数
        public StockQueryWnd()
        {
            InitializeComponent();
            ResetDataPager();
            InitBtnImage();
            this._pagerIndex = new PagerIndex(dataPager.PageCurrent, CommonDefine.iPageSize);
            if (MainForm.CurrentUser.UserRole.Trim() == "03")
            {
                this.stockQueryUC.EnableWarehouseUC(false, MainForm.CurrentUser.WarehouseCode, MainForm.CurrentUser.WarehouseName);
            }
            else
            {
                this.stockQueryUC.EnableWarehouseUC(true, MainForm.CurrentUser.WarehouseCode, MainForm.CurrentUser.WarehouseName);
            }
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnCountBill.Image = global::Zwms.Client.Properties.Resources.list ;
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.view;
            this.btnPrint.Image = global::Zwms.Client.Properties.Resources.print;
        }

        private void ResetDataPager()
        {
            dataPager.PageCurrent = 1;
            dataPager.PageSize = CommonDefine.iPageSize;
        }

        public void SetQueryState()
        {
            this.btnCountBill.Enabled = false;

        }


        //向新建盘点单各属性赋值
        private void initCountBill(BilCountBill record)
        {
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            record.Id = client.InitCountBillCode();
            record.PlanCountDate = CommonHelper.GetDateString(this._planDate);
            if (!string.IsNullOrWhiteSpace(this.stockQueryUC.tbsWarehouse.GetTextBoxContent()))
            {
                record.WarehouseName = this.stockQueryUC.tbsWarehouse.GetTextBoxContent();
                record.WarehouseCode = this.stockQueryUC.tbsWarehouse.Tag.ToString();
            }
            record.BillStatus = "0";
            record.IsDeactived = "0";
           ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
            DateTime dtNow = client1.GetCurrentSystemTime();
            record.CreateUserName = MainForm.CurrentUser.UserName;
            record.CreateUserCode = MainForm.CurrentUser.Id;
            record.CreateDate = Helper.CommonHelper.GetDateString(dtNow);
            record.CreateTime = Helper.CommonHelper.GetTimeString(dtNow);
            return;
        }


        //库存信息记录转换为盘点单明细记录
        private BilCountBillDetail recordConvert(StoGoodsStock stoRecord,string strCBCode)
        {
            if (stoRecord == null)
            {
                return null;
            }
            BilCountBillDetail cbRecord = new BilCountBillDetail();
            cbRecord.CountBillCode = strCBCode;
            cbRecord.StockCode = stoRecord.Id;
            cbRecord.IBCode = stoRecord.IBCode;
            cbRecord.IBDate = stoRecord.IBDate;
            cbRecord.WarehouseCode = stoRecord.WarehouseCode;
            cbRecord.WarehouseName = stoRecord.WarehouseName;
            cbRecord.AreaCode = stoRecord.AreaCode;
            cbRecord.AreaName = stoRecord.AreaName;
            cbRecord.LocationCode = stoRecord.LocationCode;
            cbRecord.LocationName = stoRecord.LocationName;
            cbRecord.GoodsCode = stoRecord.GoodsCode;
            cbRecord.GoodsBarCode = stoRecord.GoodsBarCode;
            cbRecord.SingleGoodsCode = stoRecord.SingleGoodsCode;
            cbRecord.GoodsName = stoRecord.GoodsName;
            cbRecord.Spec = stoRecord.GoodsSpec;
            cbRecord.PkgQty = stoRecord.PackageQty;
            cbRecord.PieceQty = stoRecord.PieceQty;
            cbRecord.GoodsQty = stoRecord.GoodsTotalQty;
            return cbRecord;
        }
        #endregion

        #region 控件函数
        private void btnQuery_Click(object sender, EventArgs e)
        {
            this.stockQueryUC.SetQueryRecord();
            this.stockDetailUC.LoadStockDetail(this.stockQueryUC.QueryRecord, ref _pagerIndex);
            this.dataPager.RecordCount = _pagerIndex.RecordCount;
        }

        private void btnCountBill_Click(object sender, EventArgs e)
        {
            if (this.stockDetailUC.dgv.RowCount <= 0)
            {
                MessageBox.Show("没有查询结果，不能按照查询结果生成盘点单！");
                return;
            }
            else 
            {
                if (MessageBox.Show("是否按照当前查询结果生成盘点单？", "确定", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    return;
            }
            PlanDateSelForm window = new PlanDateSelForm();
            if (window.ShowDialog() == DialogResult.OK)
            {
                BilCountBill newBill = new BilCountBill();
                this._planDate = window.SelectedDateTime;
                this.initCountBill(newBill);
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                if (!client.AddCountBill(newBill))
                {
                    MessageBox.Show("生成盘点单失败！");
                    return;
                }
                this.stockQueryUC.SetQueryRecord();
                PagerIndex pIndex = new PagerIndex(1, int.MaxValue);
                IList<StoGoodsStock> recordList = client.QueryGoodsStockList(this.stockQueryUC.QueryRecord, ref pIndex);
                if (recordList == null)
                    return;
                
                foreach (StoGoodsStock stoRecord in recordList)
                {
                    BilCountBillDetail billDtl = new BilCountBillDetail();
                    billDtl = this.recordConvert(stoRecord, newBill.Id);
                    bool b = client.AddCountBillDetail(billDtl);
                }
                MessageBox.Show("盘点单已成功生成，单号："+newBill.Id);
                InfoSys.SysLogWriter.WriteOptLog("生成盘点单", "NewCountBill", "新建");
            }
        }

        private void dataPager_BtnClick(object sender, EventArgs e)
        {
            _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
            this.stockQueryUC.SetQueryRecord();
            this.stockDetailUC.LoadStockDetail(this.stockQueryUC.QueryRecord, ref _pagerIndex);
            this.dataPager.RecordCount = _pagerIndex.RecordCount;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (this.stockDetailUC.dgv.RowCount <= 0)
            {
                MessageBox.Show("没有查询结果，不能按照查询结果生成盘点单！");
                return;
            }
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            PagerIndex pIndex = new PagerIndex(1, int.MaxValue);
            IList<StoGoodsStock> recordList = client.QueryGoodsStockList(this.stockQueryUC.QueryRecord, ref pIndex);
            Report.ReportHelper.PrintGoodsStock(recordList.ToList());
        }
        #endregion

    }
}
